package rpg.console;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import rpg.Location;
import rpg.RPG;
import rpg.entities.Chick;
import rpg.entities.Entity;
import rpg.entities.EntityHandler;
import rpg.entities.Player;
import static rpg.util.ColorCodes.*;

public class CommandParser {

    private HashMap<String, Command> commands;
    private String com;
    private static CommandParser instance;

    public CommandParser() {
        addCommands();
    }

    public static CommandParser getInstance() {
        if (instance == null) {
            instance = new CommandParser();
        }
        return instance;
    }

    /**
     * Ez adja hozza a parancsokat a HashMaphez
     */
    private void addCommands() {
        commands = new HashMap<String, Command>();

        commands.put("help", new Command("help", "kiirja a parancsokat", 0) {
            @Override
            public void runCommand(String[] params) {
                LinkedList<String> cmds = new LinkedList<String>(commands.keySet());
                Collections.sort(cmds);
                for (String s : cmds) {
                    Command c = commands.get(s);
                    if (c == null) {
                        continue;
                    }
                    if (c.visible) {
                        printToConsole(GREEN + s + WHITE + ": " + CYAN + c.help);
                    }
                }
            }
        });

        commands.put("ineedallcommands", new Command("ineedallcommands", "kiirja a titkos parancsokat", 0, false) {
            @Override
            public void runCommand(String[] params) {
                LinkedList<String> cmds = new LinkedList<String>(commands.keySet());
                Collections.sort(cmds);
                for (String s : cmds) {
                    Command c = commands.get(s);
                    if (c == null) {
                        continue;
                    }

                    printToConsole(GREEN + s + WHITE + ": " + CYAN + c.help);

                }
            }
        });


        commands.put("exit", new Command("exit", "kilep a játékból", 0) {
            @Override
            public void runCommand(String[] params) {
                System.exit(0);
            }
        });

        commands.put("iddqd", new Command("iddqd", "halhatatlanság", 0, false) {
            @Override
            public void runCommand(String[] params) {
                Player player = Player.getInstance();
                if (player != null) {

                    printToConsole(player.toggleImmortal() ? GREEN + "Halhatatlan lettél!" : RED + "Már nem vagy halhatatlan");
                }
            }
        });

        commands.put("version", new Command("version", "verzió kiírása", 0) {
            @Override
            public void runCommand(String[] params) {
                printToConsole("Verzió: " + GREEN + RPG.version);
            }
        });

        commands.put("clear", new Command("clear", "konzol törlése", 0) {
            @Override
            public void runCommand(String[] params) {
                Console.getInstance().clearConsole();
            }
        });

        commands.put("xp", new Command("xp", "100 xp hozzáadása", 0, false) {
            @Override
            public void runCommand(String[] params) {
                Player.getInstance().accountXp(100);
            }
        });

        commands.put("ilovekfc", new Command("ilovekfc", "indokolatlanul sok csirke spawnolása", 0, false) {
            @Override
            public void runCommand(String[] params) {
                for (int i = 0; i < 50; ++i) {
                    Location l = new Location(Player.getInstance().getCurrentLocation().getMapCoordinate(), Player.getInstance().getMap());
                    Entity entity = EntityHandler.getInstance().createEntity('c', l);
                    Player.getInstance().getMap().entities.add(entity);
                }
            }
        });
        
        commands.put("meow", new Command("meow", "indokolatlanul sok cica spawnolása", 0, false) {
            @Override
            public void runCommand(String[] params) {
                for (int i = 0; i < 50; ++i) {
                    Location l = new Location(Player.getInstance().getCurrentLocation().getMapCoordinate(), Player.getInstance().getMap());
                    Entity entity = EntityHandler.getInstance().createEntity('h', l);
                    Player.getInstance().getMap().entities.add(entity);
                }
            }
        });
        
        commands.put("ram", new Command("ram", "indokolatlanul sok kos spawnolása", 0, false) {
            @Override
            public void runCommand(String[] params) {
                for (int i = 0; i < 50; ++i) {
                    Location l = new Location(Player.getInstance().getCurrentLocation().getMapCoordinate(), Player.getInstance().getMap());
                    Entity entity = EntityHandler.getInstance().createEntity('k', l);
                    Player.getInstance().getMap().entities.add(entity);
                }
            }
        });
    }

    public void tryRun(String line) {
        line = line.trim().replaceAll("\\s+", " "); //kiszedjük belőle a fölösleges szóközöket
        String[] split = line.split("\\s");
        String cmd = split[0];
        String[] params = new String[split.length - 1];
        for (int i = 1; i < split.length; ++i) {
            params[i - 1] = split[i];
        }
        Command c = commands.get(cmd.toLowerCase());
        if (c == null) {
            printToConsole(RED + "Nincs olyan parancs hogy " + GREEN + cmd);
            return;
        }
        c.canRun(params);
    }

    private void printToConsole(String s) {
        Console c = Console.getInstance();
        c.printLine(s, 0xff5c5c5c);
    }
}
